package cn.dankal.flyaudio.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.List;

import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.model.WuLiu;
import cn.dankal.flyaudio.model.WuLiuManager;
import razerdp.basepopup.BasePopupWindow;

public class AfterSalesOrderInputPop extends BasePopupWindow implements View.OnClickListener {

    private TextView applyView;
//    private TextView inputCodeView;
    private EditText inputOrderNumView;
    private Spinner spinner;


    public AfterSalesOrderInputPop(Context context) {
        super(context);
    }


    @Override
    public View onCreateContentView() {

        return createPopupById(R.layout.pop_after_sales_input_order);
    }

    private WuLiu currentWuliu;
    private ArrayAdapter<String> adapter;
    @Override
    public void onViewCreated(View contentView) {
        super.onViewCreated(contentView);
        applyView = findViewById(R.id.tv_apply);
//        inputCodeView = findViewById(R.id.et_code);
        inputOrderNumView = findViewById(R.id.et_reason);
        spinner = findViewById(R.id.spinner);
        List<WuLiu> lius = WuLiuManager.getWuLiu();
        String s[] = new String[lius.size()];
        for (int i = 0; i < lius.size(); i++){
            s[i] = lius.get(i).getName();
        }
        adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_item, s);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setVisibility(View.VISIBLE);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                WuLiu wuLiu = WuLiuManager.getWuLiu().get(position);
                currentWuliu = wuLiu;
//                inputCodeView.setText(wuLiu.getName());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        applyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String orderNum = inputOrderNumView.getText().toString();
                if (currentWuliu == null|| TextUtils.isEmpty(orderNum)) {
                    ToastUtils.showShort("请输入完整信息!");
                    return;
                }
                if (listener != null) {
                    dismiss();
                    listener.doApply(currentWuliu.getCode(), orderNum);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
    }

    private DoInputOrderNumListener listener;

    public AfterSalesOrderInputPop setDoApplyListener(DoInputOrderNumListener listener) {
        this.listener = listener;
        return this;
    }

    public interface DoInputOrderNumListener {
        void doApply(String reason, String orderNum);
    }
}
